<!--图片上传控件，改造自mpvue-weui-->
<template>
  <div class="weui-uploader" :class="topbg === 'topbg' ? 'topbg':''">
    <div class="weui-uploader__hd" v-if="showTip">
      <div class="weui-uploader__title">图片上传</div>
      <div class="weui-uploader__info">{{files.length}}/{{maxLength}}</div>
    </div>
    <div class="weui-uploader__bd" :class="pageType === 'white-s' ? 'white-s' : ''">
      <div v-if="showType" class="weui-uploader__files" id="uploaderFiles">
        <div v-for="(item ,index) in files" :key="index">
          <div class="weui-uploader__file">
            <image class="weui-uploader__img" :src="item" mode="aspectFill" @click="previewImage" :id="item" />
            <div class="delete-icon" @click="deleteImg" :id="item" :data-index="index" :key="index"></div>
          </div>
        </div>
      </div>
      <div v-if="files.length < maxLength" class="weui-uploader__input-box">
        <div class="weui-uploader__input" @click="chooseImage"></div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isComponents: true,
        files: []
      }
    },
    onLoad() {
      // 初始化data
      Object.assign(this.$data, this.$options.data())
    },
    props: {
      uploadFiles: {
        type: Array,
        default: []
      },
      maxLength: {
        type: Number,
        default: 20
      },
      showTip: {
        type: Boolean,
        default: true
      },
      showType: {
        type: Boolean,
        default: true
      },
      topbg: {
        type: String,
        default: ''
      },
      count: {
        type: Number,
        default: 9
      },
      pageType: {
        type: String,
        default: ''
      }
    },
    mounted() {
      this.files = this.files.concat(this.uploadFiles)
    },
    methods: {
      chooseImage(e) {
        this.showType = true
        let that = this
        if (!(this.files.length > this.maxLength - 1)) {
          that.$pt.chooseImage({
            count: that.count, // 默认9
            sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
              // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
              that.files = that.files.concat(res.tempFilePaths)
              res.files = that.files
              that.$emit('upLoadSuccess', res)
            },
            fail: function(res) {
              that.$emit('upLoadFail', res)
              // 修改提交记录
            },
            complete: function(res) {
              that.$emit('upLoadComplete', res)
            }
          })
        } else {
          that.$utils.showToast({
            title: `最多上传 ${this.maxLength} 张图片`,
            icon: 'none',
            mask: true
          })
        }
      },
      previewImage(e) {
        this.$pt.previewImage({
          current: e.currentTarget.id, // 当前显示图片的http链接
          urls: this.files // 需要预览的图片http链接列表
        })
      },
      deleteImg(e) {
        Array.prototype.indexOf = function(val) { // eslint-disable-line
          for (let i = 0; i < this.length; i++) {
            if (this[i] === val) return i
          }
          return -1
        }
        Array.prototype.remove = function(val) { // eslint-disable-line
          let index = this.indexOf(val)
          if (index > -1) {
            this.splice(index, 1)
          }
        }
        this.files.remove(e.currentTarget.id)
        let uploadDeleteObj = {
          src: e.currentTarget.id,
          index: e.currentTarget.dataset.index,
          files: this.files
        }
        this.$emit('uploadDelete', uploadDeleteObj)
      }
    }
  }
</script>

<style lang="less" scoped>
  .weui-uploader__file {
    position: relative;
  }
  .weui-uploader__bd {
    overflow: visible;
    &.white-s{
      .weui-uploader__input-box{
        background: #fff;
        margin-bottom: 0;
        border: none;
        border-radius: 5px;
        &:before{
          width: 12px;
          height: 1px;
          background: #333333;
        }
        &:after{
          height: 12px;
          width: 1px;
          background: #333333;
        }
      }
    }
  }
  .delete-icon {
    position: absolute;
    width: 20px;
    height: 20px;
    background: #f43530;
    right: 0;
    top: -10px;
    border-radius: 20px;
    z-index: 5;
  }
  .delete-icon::before {
    content: '';
    width: 13px;
    height: 2px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
  }
</style>
